import Cookies from 'js-cookie';

/**
 * window.localStorage 浏览器永久缓存
 * @method set 设置永久缓存
 * @method get 获取永久缓存
 * @method remove 移除永久缓存
 * @method clear 移除全部永久缓存
 */
export let Local = {
	// 查看 v2.4.3版本更新日志
	setKey(key: string) {
		// @ts-ignore
		return `${__NEXT_NAME__}:${key}`;
	},
	// 设置永久缓存
	set<T>(key: string, val: T) {
		window.localStorage.setItem(Local.setKey(key), JSON.stringify(val));
	},
	// 获取永久缓存
	get(key: string) {
		let json = <string>window.localStorage.getItem(Local.setKey(key));
		return JSON.parse(json);
	},
	// 移除永久缓存
	remove(key: string) {
		window.localStorage.removeItem(Local.setKey(key));
	},
	// 移除全部永久缓存
	clear() {
		window.localStorage.clear();
	},
};

/**
 * window.sessionStorage 浏览器临时缓存
 * @method set 设置临时缓存
 * @method get 获取临时缓存
 * @method remove 移除临时缓存
 * @method clear 移除全部临时缓存
 */
export let Session = {
	// 设置临时缓存
	set(key: string, val: any) {
		if (key === 'token' || key === 'refresh_token') {
			Cookies.set(key, val);
		}
		window.sessionStorage.setItem(key, JSON.stringify(val));
	},
	// 获取临时缓存
	get(key: string) {
		if (key === 'token' || key === 'refresh_token') return Cookies.get(key);
		let json = <string>window.sessionStorage.getItem(key);
		return JSON.parse(json);
	},
	// 移除临时缓存
	remove(key: string) {
		if (key === 'token' || key === 'refresh_token') return Cookies.remove(key);
		window.sessionStorage.removeItem(key);
	},
	// 移除全部临时缓存
	clear() {
		// Cookies.remove('token');
		// Cookies.remove('refresh_token');
		Cookies.remove('tenantId');
		window.sessionStorage.clear();
	},
	// 获取当前存储的 token
	getToken() {
		return this.get('token');
	},
	// 获取当前的租户
	getTenant() {
		return Local.get('tenantId') ? Local.get('tenantId') : 1;
	},
};

export let crc32Dom = {
	// 字符串转32位字节
	crc32String(input: string) {
		let crcTable = [];
		let crc = 0 ^ -1;

		for (let i = 0; i < 256; i++) {
			let c = i;
			for (let j = 0; j < 8; j++) {
				if (c & 1) {
					c = 0xedb88320 ^ (c >>> 1);
				} else {
					c = c >>> 1;
				}
			}
			crcTable[i] = c;
		}

		for (let i = 0; i < input.length; i++) {
			crc = (crc >>> 8) ^ crcTable[(crc ^ input.charCodeAt(i)) & 0xff];
		}

		let num = (crc ^ -1) >>> 0;
		let numAll = num.toString(16).toUpperCase();
		if (numAll.length < 8) {
			numAll = '0' + numAll;
		}
		return numAll; //
	},
};
export let getUUID = {
	generateUUID() {
		if (typeof crypto === 'object') {
			if (typeof crypto.randomUUID === 'function') {
				return crypto.randomUUID();
			}
			if (typeof crypto.getRandomValues === 'function' && typeof Uint8Array === 'function') {
				let callback = (c: any) => {
					let num = Number(c);
					return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16);
				};
				return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, callback);
			}
		}
		let timestamp = new Date().getTime();
		let performanceNow = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0;
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
			let random = Math.random() * 16;
			if (timestamp > 0) {
				random = (timestamp + random) % 16 | 0;
				timestamp = Math.floor(timestamp / 16);
			} else {
				random = (performanceNow + random) % 16 | 0;
				performanceNow = Math.floor(performanceNow / 16);
			}
			return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
		});
	},
};
